// Beginning Game Programming
// Chapter 9
// dxaudio.cpp - DirectSound framework header file

#include "dxaudio.h"

CSoundManager *gSound;

int Init_DirectSound(HWND hwnd)
{
    HRESULT result;

    //create DirectSound manager object
    gSound = new CSoundManager();

    //initialize DirectSound
    result = gSound->Initialize(hwnd, DSSCL_PRIORITY);
    if (result != DS_OK)
        return 0;

    //set the primary buffer format
    result = gSound->SetPrimaryBufferFormat(2, 22050, 16);
    if (result != DS_OK)
        return 0;

    //return success
    return 1;
}

CSound *LoadSound(char *filename)
{
    HRESULT result;

    //create local reference to wave data
    CSound *wave;

    //attempt to load the wave file
    result = gSound->Create(&wave, filename);
    if (result != DS_OK)
        return NULL;

    //return the wave
    return wave;
}

void PlaySound(CSound *sound)
{
	if(sound != NULL){
		sound->Stop();
		sound->Play();
	}
    
}

void LoopSound(CSound *sound)
{
	if(sound != NULL){
		sound->Play(0, DSBPLAY_LOOPING);
	}
    
}

void StopSound(CSound *sound)
{
	if(sound != NULL){
		sound->Stop();
	}
}
